package xxhui.space.newsclient.Adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import xxhui.space.newsclient.Bean.NumItem;
import xxhui.space.newsclient.R;

import static xxhui.space.newsclient.util.MyApplication.TAG;

/**
 * Created by hui on 2017/7/7.
 */
//登录界面的数字键盘适配器
public class NumItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<NumItem> data;//textview的显示的字符串数据
    private OnItemClickListener clickListener;//给RecyclerView添加点击事件
    private Context context;

    public NumItemAdapter(List<NumItem> data, Context context) {
        this.data = data;
        this.context = context.getApplicationContext();
    }//初始化时注入数据

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {//设置注入item点击监听
        clickListener = itemClickListener;
    }

    /**
     * 解析布局，并注入到viewHolder
     *
     * @return 给下一步提供一个ViewHolder
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.num_item, parent, false);
        return new ViewHolder(v);
    }

    /**
     * 给ViewHolder里面的控件绑定数据
     *
     * @param holder
     * @param position
     */
    //犹豫给定图片没有数据格式，默认设置为ic_launcher
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (data != null && data.size() >= position) {
            NumItem item = data.get(position);
            ViewHolder itemHolder = ((ViewHolder) holder);
            itemHolder.num.setText(data.get(position).getNum());
            itemHolder.letter.setText(data.get(position).getLetter());
            if(position==9){
                itemHolder.view.setBackgroundResource(R.color.num_bg);
            }
            if(position==10){
                ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams)itemHolder.num.getLayoutParams();
                //<dimen name="num_item_magin_top">7dp</dimen>
                marginLayoutParams.topMargin = (int) context.getResources().getDimension(R.dimen.num_item_magin_top);
                itemHolder.letter.setVisibility(View.GONE);
            }
            if (position == 11) {
                itemHolder.num.setVisibility(View.GONE);
                itemHolder.letter.setVisibility(View.GONE);
                Drawable d = context.getResources().getDrawable(R.mipmap.num_back);
                LinearLayout layout = (LinearLayout) itemHolder.view;
                layout.setBackgroundResource(R.color.num_bg);
                ImageView imageView = new ImageView(context);
                imageView.setImageDrawable(d);
                imageView.setScaleType(ImageView.ScaleType.CENTER);//居中显示
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.gravity = Gravity.CENTER;
                //<dimen name="back_num_item_magin_top">18sp</dimen>
                lp.topMargin = (int) context.getResources().getDimension(R.dimen.back_num_item_magin_top);

                layout.addView(imageView, lp);
            }
        }
        Log.i(TAG, "onBindViewHolder: " + position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    /**
     * 内部类ViewHolder，用于保存绑定控件，如果设置了点击事件，给控件设置点击监听
     */
    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.num_item)
        TextView num;
        @BindView(R.id.letter_item)
        TextView letter;
        @BindView(R.id.num_item_layout)
        View view;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (clickListener != null) {
                clickListener.onItemClick(v, getPosition());
            }
        }
    }

    /**
     * 定义一个接口用于外部实现RecycleView的item的操作
     */
    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    public List<NumItem> getData() {
        return data;
    }

    public void setData(List<NumItem> data) {
        this.data = data;
    }

}